/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  ** This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether 
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * COPYRIGHT(c) 2018 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H__
#define __MAIN_H__

/* Includes ------------------------------------------------------------------*/

/* USER CODE BEGIN Includes */
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "oled.h"
#include "safetool.h"
#include "mode.h"

typedef struct
{
  uint32_t CanId;
  uint16_t Cmd;
  int32_t Content;
} Data;

typedef enum
{
  ACS_0,
  ACS_1,
  ACS_2
} ACS_t;


typedef enum
{
  F_NONE,
  F_START,
  F_END
} S_s;

 typedef enum
 {
 	M_NONE,
 	M_SendPos,
 	M_SetPosH,
 	M_SetPosL,
 	M_SetForce,
 	M_SetMode
 }S_m;
 
 typedef enum
 {
	E_NONE,
	E_DISLR,
	E_SPEED,
  E_EXHAUSTED
 }S_e;
 
 typedef enum
 {
  Stop,
  PullDown,
	Squat,
	DeadLife,
	Row
 }M_m;

#define TICK_DELAY 2000   //ms

#define L_MOTOR_ID 0x0001
#define R_MOTOR_ID 0x0002

#define HARDPULL_MODE 1
#define DEEPSQUART_MODE 2
#define HIGHDROP_MODE 3
#define FLATBENCHPRESS_MOED 4
#define STOP_MODE 5
#define ROW_MODE 6


#define DI1 5
#define DI3 4
#define DI4 3
#define DI8 8
#define DI9 7

extern Data datRx;
extern Data datTx;
extern uint8_t L_MOTOR;
extern uint8_t R_MOTOR;
extern uint8_t FLAG_2S;
extern uint8_t FLAG_STOP;
extern uint8_t FLAG_CAN;
extern int Count2s;
extern int Count100ms;
extern int L_Height,R_Height;

extern int encode_correction;
extern int height;
extern int get_height;
extern int Set_high;
extern int Set_low;
extern int Set_Power;
extern int Power;
extern int Power_Value;
extern int Sport_Mode ;
extern int encode,adc;
extern int Dis_O,Dis_I;
extern int point_low,point_high; 
extern uint8_t FLAG_EMERGENCY_BRAKING;
extern uint16_t MOTOR_ID;


extern S_e error;
extern S_s flag;
extern S_m mode;


#define A_GPIO GPIOB

#define Barbell_MaxLow 1500
#define Barbell_MaxHigh 15000
#define CoefficientC2E  73
#define CoefficientE2C  0.014
#define MechanicalHeight 30
/* USER CODE END Includes */

/* Private define ------------------------------------------------------------*/

#define SDO_Pin GPIO_PIN_13
#define SDO_GPIO_Port GPIOC
#define RM_Pin GPIO_PIN_0
#define RM_GPIO_Port GPIOC
#define LM_Pin GPIO_PIN_1
#define LM_GPIO_Port GPIOC
#define DIS_L_Pin GPIO_PIN_2
#define DIS_L_GPIO_Port GPIOC
#define OLED_RST_Pin GPIO_PIN_3
#define OLED_RST_GPIO_Port GPIOC
#define AIN1_Pin GPIO_PIN_0
#define AIN1_GPIO_Port GPIOA
#define DO6_Pin GPIO_PIN_2
#define DO6_GPIO_Port GPIOA
#define DIS_R_Pin GPIO_PIN_4
#define DIS_R_GPIO_Port GPIOA
#define OLED_CLK_Pin GPIO_PIN_5
#define OLED_CLK_GPIO_Port GPIOA
#define OLED_DC_Pin GPIO_PIN_6
#define OLED_DC_GPIO_Port GPIOA
#define OLED_DO_Pin GPIO_PIN_7
#define OLED_DO_GPIO_Port GPIOA
#define A2_Pin GPIO_PIN_0
#define A2_GPIO_Port GPIOB
#define A1_Pin GPIO_PIN_1
#define A1_GPIO_Port GPIOB
#define CS1_Pin GPIO_PIN_2
#define CS1_GPIO_Port GPIOB
#define SCLK_Pin GPIO_PIN_12
#define SCLK_GPIO_Port GPIOB
#define DO5_Pin GPIO_PIN_14
#define DO5_GPIO_Port GPIOB
#define EncodeB_Pin GPIO_PIN_6
#define EncodeB_GPIO_Port GPIOC
#define EncodeA_Pin GPIO_PIN_7
#define EncodeA_GPIO_Port GPIOC
#define NormalLED_Pin GPIO_PIN_8
#define NormalLED_GPIO_Port GPIOC
#define TroubleLED_Pin GPIO_PIN_9
#define TroubleLED_GPIO_Port GPIOC
#define SYNC_Pin GPIO_PIN_10
#define SYNC_GPIO_Port GPIOA
#define MOSI_Pin GPIO_PIN_15
#define MOSI_GPIO_Port GPIOA
#define MID_Pin GPIO_PIN_10
#define MID_GPIO_Port GPIOC
#define LDAC_Pin GPIO_PIN_12
#define LDAC_GPIO_Port GPIOC
#define CLR_Pin GPIO_PIN_2
#define CLR_GPIO_Port GPIOD
#define A3_Pin GPIO_PIN_3
#define A3_GPIO_Port GPIOB
#define A4_Pin GPIO_PIN_4
#define A4_GPIO_Port GPIOB
#define A5_Pin GPIO_PIN_5
#define A5_GPIO_Port GPIOB
#define A6_Pin GPIO_PIN_6
#define A6_GPIO_Port GPIOB
#define A7_Pin GPIO_PIN_7
#define A7_GPIO_Port GPIOB
#define A8_Pin GPIO_PIN_8
#define A8_GPIO_Port GPIOB
#define CS2_Pin GPIO_PIN_9
#define CS2_GPIO_Port GPIOB

/* ########################## Assert Selection ############################## */
/**
  * @brief Uncomment the line below to expanse the "assert_param" macro in the 
  *        HAL drivers code
  */
/* #define USE_FULL_ASSERT    1U */

/* USER CODE BEGIN Private defines */

#define CMD_START 0x0002
#define CMD_HEIGHT 0x0001
#define CMD_SAFE 0x0003
#define CMD_BACK 0x0004

#define bool char
#define true 1
#define false 0


#define SetStop   0
#define SendPos   1
#define SetPosH   2
#define SetPosL   3
#define SetForce  4
#define SetMode   5



#define NormalLED(x) HAL_GPIO_WritePin(NormalLED_GPIO_Port, NormalLED_Pin, (GPIO_PinState)(!x))
#define TroubleLED(x) HAL_GPIO_WritePin(TroubleLED_GPIO_Port, TroubleLED_Pin, (GPIO_PinState)(!x))

#define Tick_Enable(x) SysTick->CTRL  = 	SysTick_CTRL_CLKSOURCE_Msk | \
																					SysTick_CTRL_TICKINT_Msk   | \
																					x;                         
#define FLAG_LOWSP 0

/* USER CODE END Private defines */

#ifdef __cplusplus
 extern "C" {
#endif
void _Error_Handler(char *, int);

#define Error_Handler() _Error_Handler(__FILE__, __LINE__)
#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H__ */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
